package com.hxzy.shop.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.shop.constant.AuthConstant;
import com.hxzy.shop.entity.MemberStatus;
import com.hxzy.shop.member.dto.memberCard.resp.MemberCardListsResp;
import com.hxzy.shop.member.mapper.MemberStatusMapper;
import com.hxzy.shop.member.dto.memberCard.req.MemberCardListsReq;
import com.hxzy.shop.entity.MemberCard;
import com.hxzy.shop.member.mapper.RightsInnerInterestsMapper;
import com.hxzy.shop.member.service.MemberCardService;
import com.hxzy.shop.member.mapper.MemberCardMapper;
import com.hxzy.shop.member.vo.MemberCardListsVo;
import com.hxzy.shop.member.vo.RightsInnerInterestsVo;
import com.hxzy.shop.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【member_card(会员卡表)】的数据库操作Service实现
* @createDate 2023-08-02 14:48:08
*/
@Service
public class MemberCardServiceImpl extends ServiceImpl<MemberCardMapper, MemberCard>
    implements MemberCardService{

    @Autowired
    private MemberCardMapper memberCardMapper;

    @Autowired
    private MemberStatusMapper memberStatusMapper;

    @Override
    public Map<String, Object> memberCardLists(MemberCardListsReq req) {
        List<MemberCardListsResp> list;

        //获得会员卡表
        List<MemberCardListsVo> memberCardListsVos =  memberCardMapper.memberCardLists(req);

        if (ObjectUtil.isNotEmpty(memberCardListsVos)){
            //处理会员卡重复
            list = memberCardListsVos.stream().map(e ->{
                MemberCardListsResp memberCardListsResp = new MemberCardListsResp();
                memberCardListsResp.setId(e.getId());
                memberCardListsResp.setEnableStatus(e.getEnableStatus());
                memberCardListsResp.setGradeName(e.getGradeName());
                memberCardListsResp.setStartValueOfGrowthRange(e.getStartValueOfGrowthRange());
                memberCardListsResp.setEndValueOfGrowthRange(e.getEndValueOfGrowthRange());
                memberCardListsResp.setUpdateTime(e.getUpdateTime());
                return memberCardListsResp;
            }).collect(Collectors.toList());

            //处理会员卡权益
            list.forEach(resp ->{
                List<RightsInnerInterestsVo> rightsInnerInterestsVos = memberCardListsVos.stream().
                        filter(memberCardListsVo -> memberCardListsVo.getId().equals(resp.getId()))
                        .map(memberCardListsVo -> {
                            RightsInnerInterestsVo rightsInnerInterestsVo = null;
                            if (ObjectUtil.isNotEmpty(memberCardListsVo.getRightsId()) || ObjectUtil.isNotEmpty(memberCardListsVo.getTitleOfEquity())) {
                                rightsInnerInterestsVo = new RightsInnerInterestsVo(memberCardListsVo.getRightsId(), memberCardListsVo.getTitleOfEquity());
                            }
                            return rightsInnerInterestsVo;
                        }).collect(Collectors.toList());
                if (!rightsInnerInterestsVos.isEmpty()){
                    resp.setRightsAndInterests(rightsInnerInterestsVos);
                }
            });
        }
            String merchantId = WebUtil.getRequest().getHeader(AuthConstant.MERCHANT_ID);
            LambdaQueryWrapper<MemberStatus> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MemberStatus::getMerchantId,merchantId);
            MemberStatus memberStatus = memberStatusMapper.selectOne(queryWrapper);
            if (memberStatus==null){
                Sequence sequence = new Sequence();
                memberStatus = new MemberStatus();
                memberStatus.setMemberStatusId(sequence.nextId());
                memberStatus.setEnableStatus(false);
                memberStatus.setCreateTime(new Date());
                memberStatus.setMerchantId(Long.parseLong(merchantId));
                memberStatusMapper.insert(memberStatus);
            }
            Map<String,Object> map = new HashMap<>();
            map.put("status",memberStatus.getEnableStatus());
            if (ObjectUtil.isEmpty(map)){
                map.put("status",false);
                memberStatus.setEnableStatus(false);
                memberStatusMapper.updateById(memberStatus);
            }
            map.put("list",memberCardListsVos);

            return map;
    }
}




